<?php


namespace app\api\controller;


use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Login extends Controller
{
    /**
     * 小程序登录
     */
    public function dologin(){
        $common = new Common();
        try {
            $project_user = Session::get("project_user");
            if(!empty($project_user)){
                $black['user'] = $project_user;
                $black['code'] = 0;
                $black['msg'] = "登录成功";
                return json_encode($black);
            }

            $rule = [
                ["mobile", "require", "手机号不能为空"],
                ["password", "require", "密码不能为空"],
                ["appid", "require", "Appid不能为空"],
            ];
            $data = $this->request->post();
            $common->condition($data,$rule);
            $appid = $data['appid'];
            $config = Db::name("config")->where(['xcx_appid' => $appid])->find();
            $accountData['uniacid'] = $config['id'];
            $accountData['mobile'] = $data['mobile'];
            $accountData['password'] = md5($data['password']);
            $accountData['status'] = 1;
            $account = Db::name("project_account")->where($accountData)->field("id,name,mobile,fans_id,survey,statisticsAuto,expose")->find();
            if (empty($account)) {

//                $accountData1['uniacid'] = $config['id'];
//                $accountData1['name'] = $data['mobile'];
//                $accountData1['password'] = md5($data['password']);
//                $accountData1['status'] = 1;
//                $account = Db::name("project_account")->where($accountData)->field("id,name,mobile")->find();
//                if(empty($account)){
//                    throw new Exception("账号不存在或密码错误");
//                }

                throw new Exception("账号不存在或密码错误");
            } else {
                $project_user = Session::get("project_user");
                if(!empty($project_user)){
                    Session::clear();
                }

                //更新最近登录时间
                Db::name("project_account")->where($accountData)->update(['last_time'=>date("Y-m-d H:i:s",time())]);
                Session::set("project_user", $account);
                Session::set("config",$config);
            }
            $black['user'] = $account;
            $black['code'] = 0;
            $black['msg'] = "登录成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 小程序登录
     */
    public function dologin1(){
        $common = new Common();
        try {
            $project_user = Session::get("project_user");
            if(!empty($project_user)){
                $black['user'] = $project_user;
                $black['code'] = 0;
                $black['msg'] = "登录成功";
                $black['session'] =  $project_user;
                return json_encode($black);
            }

            $rule = [
                ["mobile", "require", "手机号不能为空"],
                ["password", "require", "密码不能为空"],
                ["appid", "require", "Appid不能为空"],
            ];
            $data = $this->request->post();
            $common->condition($data,$rule);
            $appid = $data['appid'];
            $config = Db::name("config")->where(['xcx_appid' => $appid])->find();
            $accountData['uniacid'] = $config['id'];
            $accountData['mobile'] = $data['mobile'];
            $accountData['password'] = md5($data['password']);
            $accountData['status'] = 1;
            $account = Db::name("project_account")->where($accountData)->field("id,name,mobile")->find();
            if (empty($account)) {

//                $accountData1['uniacid'] = $config['id'];
//                $accountData1['name'] = $data['mobile'];
//                $accountData1['password'] = md5($data['password']);
//                $accountData1['status'] = 1;
//                $account = Db::name("project_account")->where($accountData)->field("id,name,mobile")->find();
//                if(empty($account)){
//                    throw new Exception("账号不存在或密码错误");
//                }

                throw new Exception("账号不存在或密码错误");
            } else {

                //更新最近登录时间
                Db::name("project_account")->where($accountData)->update(['last_time'=>date("Y-m-d H:i:s",time())]);
                Session::set("project_user", $account);
//                setcookie("project_user",json_encode($account),24*3600*7);
            }
            $black['user'] = $account;
            $black['code'] = 0;
            $black['msg'] = "登录成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


}